﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using Tesla.Math;

namespace Tesla.Graphics {
    /// <summary>
    /// A light that is considered infinitely away from the viewer, thus it only
    /// has a direction (e.g. the Sun relative to the Earth seems "infinitely" away). Also
    /// known as a parallel light. This will ignore the attenuation properties of the Light base class.
    /// </summary>
    public class DirectionalLight : Light {
        private Vector3 _direction;
        private LightType _lightType = LightType.Directional;

        /// <summary>
        /// Gets or sets the direction of this light. The value
        /// is always normalized.
        /// 
        /// Default: (0, 0, -1)
        /// </summary>
        public Vector3 Direction {
            get {
                return _direction;
            }
            set {
                _direction = value;
                _direction.Normalize();
            }
        }

        /// <summary>
        /// Gets the type of light, LightType.Directional.
        /// </summary>
        public override LightType LightType {
            get {
                return _lightType;
            }
        }

        /// <summary>
        /// Creates a new DirectionalLight with direction
        /// pointing down the negative Z-axis.
        /// </summary>
        public DirectionalLight()
            : base("DirectionalLight") {
                _direction = Vector3.Forward;
        }

        /// <summary>
        /// Creates a new DirectionalLight.
        /// </summary>
        /// <param name="direction">Direction of the light</param>
        public DirectionalLight(Vector3 direction)
            : base("DirectionalLight") {
                _direction = direction;
        }

        /// <summary>
        /// Convienance method for setting the direction of this
        /// directional light. The direction is automatically 
        /// normalized.
        /// </summary>
        /// <param name="x">X component</param>
        /// <param name="y">Y component</param>
        /// <param name="z">Z component</param>
        public void SetDirection(float x, float y, float z) {
            _direction = new Vector3(x, y, z);
            _direction.Normalize();
        }

        /// <summary>
        /// Serializes this DirectionalLight.
        /// </summary>
        /// <param name="output">Output to be written to</param>
        public override void Write(Content.ISavableWriter output) {
            base.Write(output);
            output.Write("LightType", (int) _lightType);
            output.Write("Direction", _direction);
        }

        /// <summary>
        /// Deserializes this DirectionalLight.
        /// </summary>
        /// <param name="input">Input to read from</param>
        public override void Read(Content.ISavableReader input) {
            base.Read(input);
            _lightType = (LightType) input.ReadInt();
            _direction = input.ReadVector3();
        }
    }
}
